package ipmsg.gui;

import javax.swing.GroupLayout;

public class AddedUsers extends javax.swing.JFrame
{
  private javax.swing.JList guiManual;
  private javax.swing.JButton jButton1;
  private javax.swing.JButton jButton2;
  private javax.swing.JButton jButton3;
  private javax.swing.JButton jButton4;
  private javax.swing.JPanel jPanel1;
  private javax.swing.JPanel jPanel3;
  private javax.swing.JPanel jPanel4;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JTextField jTextField1;
  private javax.swing.JLabel warning;
  
  public AddedUsers() {
    ipmsg.etc.GlobalVar.WINDOW_REG.add("addedUser");
    initComponents();
    setTitle("管理好友");
    this.guiManual.setListData(ipmsg.etc.GlobalVar.ADDED_USER);
    refreshGui("当前共有 " + ipmsg.etc.GlobalVar.ADDED_USER.size() + " 好友！");
  }
  








  private void initComponents()
  {
    this.jPanel4 = new javax.swing.JPanel();
    this.jButton2 = new javax.swing.JButton();
    this.jButton3 = new javax.swing.JButton();
    this.jButton4 = new javax.swing.JButton();
    this.jPanel1 = new javax.swing.JPanel();
    this.jScrollPane1 = new javax.swing.JScrollPane();
    this.guiManual = new javax.swing.JList();
    this.jPanel3 = new javax.swing.JPanel();
    this.jTextField1 = new javax.swing.JTextField();
    this.jButton1 = new javax.swing.JButton();
    this.warning = new javax.swing.JLabel();
    
    setDefaultCloseOperation(0);
    setMinimumSize(new java.awt.Dimension(314, 452));
    addWindowListener(new java.awt.event.WindowAdapter() {
      public void windowClosing(java.awt.event.WindowEvent evt) {
        AddedUsers.this.formWindowClosing(evt);
      }
      
    });
    this.jButton2.setText("删除");
    this.jButton2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        AddedUsers.this.jButton2ActionPerformed(evt);
      }
      
    });
    this.jButton3.setText("清空");
    this.jButton3.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        AddedUsers.this.jButton3ActionPerformed(evt);
      }
      
    });
    this.jButton4.setText("关闭");
    this.jButton4.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        AddedUsers.this.jButton4ActionPerformed(evt);
      }
      
    });
    GroupLayout jPanel4Layout = new GroupLayout(this.jPanel4);
    this.jPanel4.setLayout(jPanel4Layout);
    jPanel4Layout.setHorizontalGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel4Layout.createSequentialGroup().addContainerGap(-1, 32767).addComponent(this.jButton4).addContainerGap()).addGroup(jPanel4Layout.createSequentialGroup().addContainerGap().addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(this.jButton3, javax.swing.GroupLayout.Alignment.TRAILING).addComponent(this.jButton2, javax.swing.GroupLayout.Alignment.TRAILING)).addContainerGap(-1, 32767)));
    











    jPanel4Layout.setVerticalGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup().addContainerGap(249, 32767).addComponent(this.jButton2).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(this.jButton3).addGap(18, 18, 18).addComponent(this.jButton4).addContainerGap()));
    










    getContentPane().add(this.jPanel4, "After");
    
    this.jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("好友列表"));
    
    this.jScrollPane1.setViewportView(this.guiManual);
    
    GroupLayout jPanel1Layout = new GroupLayout(this.jPanel1);
    this.jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(this.jScrollPane1, -1, 213, 32767));
    


    jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(this.jScrollPane1, -1, 323, 32767));
    



    getContentPane().add(this.jPanel1, "Center");
    
    this.jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("添加"));
    
    this.jTextField1.setText("在此输入ip");
    this.jTextField1.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mousePressed(java.awt.event.MouseEvent evt) {
        AddedUsers.this.jTextField1MousePressed(evt);
      }
      
    });
    this.jButton1.setText("添加");
    this.jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        AddedUsers.this.jButton1ActionPerformed(evt);
      }
      
    });
    GroupLayout jPanel3Layout = new GroupLayout(this.jPanel3);
    this.jPanel3.setLayout(jPanel3Layout);
    jPanel3Layout.setHorizontalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup().addComponent(this.jTextField1, -1, 217, 32767).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(this.jButton1).addContainerGap()));
    






    jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(this.jButton1).addComponent(this.jTextField1, -2, -1, -2)));
    





    getContentPane().add(this.jPanel3, "First");
    
    this.warning.setText(" 状态栏");
    getContentPane().add(this.warning, "Last");
    
    java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    setBounds((screenSize.width - 314) / 2, (screenSize.height - 452) / 2, 314, 452);
  }
  
  private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)
  {
    if (this.guiManual.getSelectedValues().length == 0) {
      this.warning.setText("请选择好友记录！");
      return;
    }
    int[] tmpSelected = this.guiManual.getSelectedIndices();
    for (int i = tmpSelected.length - 1; i >= 0; i--) {
      ipmsg.etc.GlobalVar.ADDED_USER.remove(tmpSelected[i]);
    }
    refreshGui("删除 " + tmpSelected.length + " 个好友！");
  }
  
  private void jButton3ActionPerformed(java.awt.event.ActionEvent evt)
  {
    if (!UtilityGui.confirm(this, "清空好友", "您确定要清空好友列表？")) {
      return;
    }
    ipmsg.etc.GlobalVar.ADDED_USER.clear();
    refreshGui("您清空了好友列表");
  }
  
  private void jButton4ActionPerformed(java.awt.event.ActionEvent evt)
  {
    exit();
  }
  
  private void jTextField1MousePressed(java.awt.event.MouseEvent evt)
  {
    this.jTextField1.setText("");
  }
  
  private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
  {
    String tmpIp = this.jTextField1.getText().trim();
    this.jTextField1.setText("在此输入ip");
    
    if ((tmpIp == null) || (tmpIp.equals(""))) {
      this.warning.setText("请输入好友ip地址！");
      return;
    }
    if (!ipmsg.etc.UtilityGlobal.isIP(tmpIp)) {
      this.warning.setText("请输入正确的ip地址！");
      return;
    }
    if (ipmsg.etc.GlobalVar.ADDED_USER.contains(tmpIp)) {
      this.warning.setText("该IP地址已经存在于好友列表！");
      return;
    }
    ipmsg.etc.GlobalVar.ADDED_USER.add(tmpIp);
    refreshGui("将好友 \"" + tmpIp + " \"添加至好友列表！");
  }
  
  private void formWindowClosing(java.awt.event.WindowEvent evt)
  {
    exit();
  }
  


  public static void main(String[] args)
  {
    java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
        new AddedUsers().setVisible(true);
      }
    });
  }
  













  private void refreshGui(String str)
  {
    this.guiManual.setListData(ipmsg.etc.GlobalVar.ADDED_USER);
    this.warning.setText(str);
  }
  
  private void exit() {
    ipmsg.etc.GlobalVar.WINDOW_REG.remove("addedUser");
    dispose();
  }
}
